<?php
    include_once("../options/core.php");    

    class Install
    {
        public function Install()
        {            
            $this->core = new Core();
        }

        private $core;

        private function unique_salt() 
        {  
            return substr(sha1(mt_rand()),0,22);
        } 

        private function post()
        {
            
            if(!isset($_POST["command"]))return;
            switch($_POST["command"])
                    {
                        case "0":
                                $name = $_POST["name"];
                                $email = $_POST["email"];
                                $mysql_server = $_POST["mysql_server"];
                                $mysql_db_name = $_POST["mysql_db_name"];
                                $mysql_user = $_POST["mysql_user"];
                                $mysql_password =$_POST["mysql_password"];

                                $this->core->addConfigParam("title",$name);
                                $this->core->addConfigParam("admin_email",$email);
                                $this->core->addConfigParam("mysql_server",$mysql_server);
                                $this->core->addConfigParam("mysql_db_name",$mysql_db_name);
                                $this->core->addConfigParam("mysql_user",$mysql_user);
                                $this->core->addConfigParam("mysql_password",$mysql_password);
                                $this->core->addConfigParam("unique_salt",$this->unique_salt());
                                $this->core->addConfigParam("template_name","Clear");
                                $this->core->addConfigParam("template_main","main.tpl");
                                $this->core->saveConfig();  
                                echo("Настройки сохранены...");                              
                                break;
                        case "1":
                                $query = "DROP TABLE IF EXISTS Users;";
                                $this->core->queryToDataBase($query);  
                                echo("Подготовка...");                          
                                break;
                        case "2":
                                $query = "CREATE TABLE Users(
                                    id INT(10) AUTO_INCREMENT,
                                    email VARCHAR(100) NOT NULL UNIQUE KEY,
                                    bdate DATE,
                                    fname VARCHAR(100) NOT NULL,
                                    lname VARCHAR(100) NOT NULL,
                                    photo VARCHAR(200),
                                    permission INT(10) NOT NULL,
                                    pass VARCHAR(200) NOT NULL,
                                    PRIMARY KEY(id)
                                );";     
                                $this->core->queryToDataBase($query);
                                echo("Создание таблицы пользователей...");                       
                                break;
                         case "3":
                             $fname = $_POST["fname"];
                             $lname = $_POST["lname"];
                             $email = strtolower($_POST["email"]);
                             $password = sha1($this->core->getConfigParam("unique_salt").$_POST["password"]);
                             $avatar = "";
                             if(isset($_FILES['avatar'])){
                                 $avatar = $this->core->addFile($_FILES['avatar']);
                             }
                             //echo($_POST['avatar']['name']);                             
                             $query="INSERT INTO Users (email,fname,lname,photo,permission,pass) VALUES ('".$email."','".$fname."','".$lname."','".$avatar."',1,'".$password."');";
                             $this->core->queryToDataBase($query);
                             echo("Администратор создан..");
                             break;
                         case "4":
                             $query = "SELECT * FROM Users WHERE id=1;";
                             $res = $this->core->queryToDataBase($query);
                             $res->data_seek(0);
                             $row = $res->fetch_assoc();
                             $content = "<table><tr><td rowspan='2'>";
                             $content .= "<div style='background:url(\"".$row['photo']."\");background-size: cover; background-position: center center; width:150px;height:150px;border-radius:150px;'></td><td>";
                             $content .="<h1>     ".$row['fname']."</h1></td></tr><tr><td>";
                             $content .="<h1>     ".$row['lname']."</h1></td></tr></table>";
                             echo $content;
                             break;

                    } 
                               
                   
        }

        public function main()
        {
            try
            {
                
                if(isset($_POST["command"]))
                {
                    $this->post();
                }
                else
                {
                   
                    echo $this->core->init();
                }                                
                
            }
            catch(Exception $ex)
            {
                echo($ex->getMessage());
            }
            
        }
    }
   
    try{
        $install = new Install();
        $install->main();
    }catch(Exception $ex){
        echo($ex->getMessage());
    }
    
?>
